/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import org.openeccos.EccosAppInstance;
import org.openeccos.util.ILookAndFeel;
import org.openeccos.util.EccosDefaultLookAndFeel;
import org.openeccos.util.PDUtil;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.app.Border;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.FillImage;
import nextapp.echo2.app.FillImageBorder;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.LayoutDirection;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.WindowPane;

/**
 * A gereral purpose floating window
 *
 * @author cgspinner@web.de
 */
public abstract class PDWindowPane extends WindowPane {

	protected ILookAndFeel colorScheme = new EccosDefaultLookAndFeel();
	protected Row toolbar;

	// Echo is disposing the window and recreating it
	// when something like change the title happens.
	// The problem is that when it recreates the window
	// it calls to EchoPDWindowPane.ZIndexManager.add
	// and then to EchoPDWindowPane.ZIndexManager.raise
	public boolean initialized = false;

	public PDWindowPane() {
		initGUI();
	}

	public void update() {
		// Empty: Callback used to update a form when other
		// form is performing any related change
	}

	private void initGUI() {
		setClosable(true);
		setWidth(new Extent(500));
		setHeight(new Extent(450));
		setInsets(new Insets(0, 0, 0, 0));
		setIconInsets(new Insets(7, 3, 0, 0));
		setBackground(EccosDefaultLookAndFeel.LIGHT_BACKGROUND_COLOR);

		setTitleFont(new Font(Font.VERDANA, Font.BOLD, new Extent(16)));
		setTitleHeight(new Extent(28));
		setTitleBackgroundImage(new FillImage(PDUtil.getImage("img/footerBackground.png"), null, null,
				FillImage.REPEAT_HORIZONTAL));
		setTitleInsets(new Insets(25, 5, 0, 0));

		FillImageBorder border = null;
		if (((EccosAppInstance) ApplicationInstance.getActive()).isIE()) {
			//IE6 does not support semitransparent images... :(
			//show a simple frame instead
			border = new FillImageBorder(Color.DARKGRAY, new Insets(2, 1, 2, 2), new Insets(2, 0, 2, 2));
		} else {
			border = new FillImageBorder(null, new Insets(10), new Insets(6));
			border.setFillImage(FillImageBorder.TOP_LEFT, new FillImage(PDUtil.getImage(
					"img/border/BorderTopLeft.png"), null, null, FillImage.REPEAT));
			border.setFillImage(FillImageBorder.TOP, new FillImage(PDUtil.getImage(
					"img/border/BorderTop.png"), null, null, FillImage.REPEAT));
			border.setFillImage(FillImageBorder.TOP_RIGHT, new FillImage(PDUtil.getImage(
					"img/border/BorderTopRight.png"), null, null, FillImage.REPEAT));
			border.setFillImage(FillImageBorder.LEFT, new FillImage(PDUtil.getImage(
					"img/border/BorderLeft.png"), null, null, FillImage.REPEAT));
			border.setFillImage(FillImageBorder.RIGHT, new FillImage(PDUtil.getImage(
					"img/border/BorderRight.png"), null, null, FillImage.REPEAT));
			border.setFillImage(FillImageBorder.BOTTOM_LEFT, new FillImage(PDUtil.getImage(
					"img/border/BorderBottomLeft.png"), null, null, FillImage.REPEAT));
			border.setFillImage(FillImageBorder.BOTTOM, new FillImage(PDUtil.getImage(
					"img/border/BorderBottom.png"), null, null, FillImage.REPEAT));
			border.setFillImage(FillImageBorder.BOTTOM_RIGHT, new FillImage(PDUtil.getImage(
					"img/border/BorderBottomRight.png"), null, null, FillImage.REPEAT));
		}
		setBorder(border);
	}

	public Button addToolButton(String text, String image, boolean showText) {
		Button btn = new Button();
		if (image != null) {
			btn.setIcon(PDUtil.getImage(image));
		}
		btn.setBorder(new Border(0, null, 0));
		btn.setRolloverBorder(new Border(1, Color.DARKGRAY, Border.STYLE_DOTTED));
		btn.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, new Extent(9)));
		btn.setLineWrap(false);
		btn.setForeground(Color.WHITE);
		btn.setRolloverBackground(colorScheme.getBackgroundDark());
		btn.setIconTextMargin(new Extent(2));
		btn.setInsets(new Insets(0, 3, 0, 0));
		if (showText) {
			btn.setText(text);
		} else {
			btn.setToolTipText(text);
		}
		getToolbar().add(btn);
		return btn; // we return the btn, so it is easy to attach a listener
	}

	public Row getToolbar() {
		if (toolbar == null) {
			toolbar = new Row();
			toolbar.setCellSpacing(new Extent(6));
			toolbar.setInsets(new Insets(0, 3, 45, 0));
			toolbar.setLayoutDirection(LayoutDirection.RTL);
			toolbar.setAlignment(new Alignment(Alignment.RIGHT, Alignment.TOP));
			add(toolbar);
		}
		return toolbar;
	}

	// we need to override, as a work-around for adding the toolbar
	@Override
	public boolean isValidChild(Component component) {
		return true;
	}
}
